package com.atguigu.gmall.ums.service.impl;

import com.atguigu.core.exception.MemberException;
import com.zhenzi.sms.ZhenziSmsClient;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.core.bean.PageVo;
import com.atguigu.core.bean.Query;
import com.atguigu.core.bean.QueryCondition;

import com.atguigu.gmall.ums.dao.MemberDao;
import com.atguigu.gmall.ums.entity.MemberEntity;
import com.atguigu.gmall.ums.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public PageVo queryPage(QueryCondition params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageVo(page);
    }

    @Override
    public Boolean checkData(String data, Integer type) {
        QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
        switch (type) {
            case 1:
                wrapper.eq("username", data);
                break;
            case 2:
                wrapper.eq("mobile", data);
                break;
            case 3:
                wrapper.eq("email", data);
                break;
            default:
                return false;
        }

        return this.count(wrapper) == 0;
    }

    @Override
    public String register(MemberEntity memberEntity, String code) {
        //1.校验手机验证码

        //1.2获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(memberEntity.getMobile());
        //1.3比对,redis中的验证码是否和前端传来的一样
        if (!redisCode.equals(DigestUtils.md5Hex(code))) {
            return "验证码错误";
        }

        //生成盐
        String salt = UUID.randomUUID().toString().substring(0, 6);
        memberEntity.setSalt(salt);
        //加盐加密
        memberEntity.setPassword(DigestUtils.md5Hex(memberEntity.getPassword() + salt));
        //新增用户
        memberEntity.setGender(0);
        memberEntity.setIntegration(0);
        memberEntity.setLevelId(0L);
        memberEntity.setCreateTime(new Date());
        memberEntity.setStatus(1);
        this.save(memberEntity);

        //删除redis中的验证码
        redisTemplate.delete(memberEntity.getMobile());

        return "注册成功";
    }

    @Override
    public void code(String phone) {
        //初始化ZhenziSmsClient
        ZhenziSmsClient client = new ZhenziSmsClient("https://sms_developer.zhenzikj.com", "105460", "96f5f3e6-a94d-494c-9504-3b246c08b42b");
        //生成6位验证码
        int code = (int) ((Math.random() * 9 + 1) * 100000);

        Map<String, String> params = new HashMap<String, String>();
        params.put("message", "验证码为:" + code);
        params.put("number", phone);
        try {
            //发送短信
            //String result = client.send(params);
            //System.out.println(result);
            System.out.println(code);
            //保存验证码到redis
            redisTemplate.opsForValue().set(phone, DigestUtils.md5Hex(String.valueOf(code)), 5, TimeUnit.MINUTES);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public MemberEntity queryUser(String username, String password) {
        //1.根据用户名查询用户
        MemberEntity memberEntity = this.getOne(new QueryWrapper<MemberEntity>().eq("username", username));
        //1.2判断用户是否存在
        if (memberEntity==null) {
            throw new MemberException("用户名不存在");
        }
        //2.对前端传来的密码进行加密加盐
        password = DigestUtils.md5Hex(password + memberEntity.getSalt());
        //3.和后端进行密码比对
        if (!StringUtils.equals(password,memberEntity.getPassword())) {
            throw new MemberException("密码错误");
        }
        return memberEntity;
    }


}